package com.hzqy.web.ac;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Base64;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hzqy.commons.utils.*;
import com.hzqy.persistence.dao.hotel.HotelAdvertiseDao;
import com.hzqy.service.system.SysConfigServiceBeen;
import com.hzqy.web.vo.*;
import com.hzqy.web.vo.system.SysConfigVo;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.hzqy.commons.utils.ExcelUtil.MyEntry;
import com.hzqy.service.catalog.CatalogServiceBean;
import com.hzqy.service.content.ContentServiceBean;
import com.hzqy.service.pms.PmsServiceBean;
import com.hzqy.web.vo.content.CmContentLedgerVo;

@Controller
@RequestMapping("/")
public class ContentAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());

	private final String picTypeContent = "内容";
	private final String picTypeSeries = "剧集";
	private final String picTypeModel = "模板";
	private static final String[] allowedFileType = new String[]{"png", "jpg", "gif","zip","jar","tar","docx","doc","xlsx","xls","flv","mp4","mp3","mpg","avi","mov","wmv"};
	@Resource
	private ContentServiceBean contentServiceBean;
	@Resource
	private PmsServiceBean pmsServiceBean;

	@Resource
	private CatalogServiceBean catalogServiceBean;

	@Resource
	private SysConfigServiceBeen systemConfigService;

	@Resource
	private HotelAdvertiseDao hotelAdvertiseDao;


	@RequestMapping("/content/content_list")
	@ResponseBody
	public Object contentList(ModelMap map,CmContentVo cmContentVo, HttpSession session, int page, int rows) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getAuthorityPmsOrgIds(userVo);
			if(cmContentVo.getF_cc_companyid()==null||"".equals(cmContentVo.getF_cc_companyid())||cmContentVo.getF_cc_companyid().equals(orgIds)){
				cmContentVo.setF_cc_companyid(orgIds);
			}else{
				boolean flag=true;
				String[] orgIdlist=orgIds.split(",");
				for(int i=0;i<orgIdlist.length;i++){
					if(cmContentVo.getF_cc_companyid().equals(orgIdlist[i])){
						flag=false;
						break;
					}
				}
				if(flag){//该企业不在列表内则不可查询
					cmContentVo.setF_cc_companyid(orgIds);
				}

			}
		}
		Map<String,Object> contentListMap = new HashMap<String, Object>();
		contentListMap =  contentServiceBean.selectContentList(cmContentVo, page, rows);
		return contentListMap;
	}

	@RequestMapping("/content/series_list")
	@ResponseBody
	public Object contentList(ModelMap map,CmSeriesVo cmSeriesVo, int page, int rows) {
		Map<String,Object> seriesListMap = new HashMap<String, Object>();
		seriesListMap = contentServiceBean.selectSeriesList(cmSeriesVo , page ,rows);
		return seriesListMap;
	}

	@RequestMapping("/content/delete_content")
	@ResponseBody
	public Object deleteContent(int f_cc_id, HttpSession session) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		CmContentVo cmContentVo=contentServiceBean.selectContent(f_cc_id);
		CmContentLedgerVo cmContentLedger=new CmContentLedgerVo();
		cmContentLedger.setF_ccl_ip(String.valueOf(session.getAttribute(ConstantUtils.SESSION_USER_IP)));
		cmContentLedger.setF_ccl_user(userVo.getF_pu_name());
		cmContentLedger.setF_ccl_contentid(f_cc_id);
		cmContentLedger.setF_ccl_contentName(cmContentVo.getF_cc_name());
		cmContentLedger.setF_ccl_type(6);

		int flag = contentServiceBean.deleteContent(f_cc_id);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","删除成功!");
			if(cmContentVo.getF_cc_type()==0
					||cmContentVo.getF_cc_type()==2
					||cmContentVo.getF_cc_type()==3
					||cmContentVo.getF_cc_type()==4
					||cmContentVo.getF_cc_type()==5
					||cmContentVo.getF_cc_type()==110){//业务账户不记录台账
				contentServiceBean.insertCmContentLadger(cmContentLedger);//记录操作台账
			}
		} else {
			resultMap.put("success", false);
			resultMap.put("result","删除失败!");
		}
		return resultMap;
	}

	/**
	 * 批量生效与失败/删除等功能
	 * @param f_ssc_ids
	 * @return
	 */
	@RequestMapping("/content/delete_series")
	@ResponseBody
	public Object deleteSeries(int f_ssc_status,String f_ssc_ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.deleteSeries(f_ssc_status,f_ssc_ids);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","删除成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","删除失败!");
		}
		return resultMap;
	}

	@RequestMapping("/content/insert_content")
	@ResponseBody
	public Object insertContent(CmContentVo cmContentVo,String picJson, HttpSession session) throws UnsupportedEncodingException {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		int orgId = 0;
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			orgId=userVo.getOrgId();
		}
		CmContentLedgerVo cmContentLedger=new CmContentLedgerVo();
		cmContentLedger.setF_ccl_ip(String.valueOf(session.getAttribute(ConstantUtils.SESSION_USER_IP)));
		cmContentLedger.setF_ccl_user(userVo.getF_pu_name());
		cmContentVo.setCmContentLedger(cmContentLedger);
		cmContentVo.setOrgId(orgId);
		cmContentVo.setOrgIds(userVo.getOrgIds());
		if (cmContentVo.getF_cc_encryption() == 1) {
			//解码
			Base64.Decoder decoder = Base64.getDecoder();
			byte[] bytes = decoder.decode(cmContentVo.getF_cc_desc());
			String F_cc_desc_decode = new String(bytes, "UTF-8");
			cmContentVo.setF_cc_desc(F_cc_desc_decode);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>();

		if(cmContentVo.getF_cc_type()==100){
			int existingAccount = contentServiceBean.verificationExistingAccounts(cmContentVo,1);
			if(existingAccount==0){
				if ("".equals(picJson)) {
					picJson = "[]";
				}
				resultMap = contentServiceBean.insertContent(cmContentVo,picJson);
			}else{
				resultMap.put("success", false);
				resultMap.put("result","用户数录入已超过"+existingAccount+"个，若要继续录入，需向省公司发起申请！");
			}
		}else{
			if ("".equals(picJson)) {
				picJson = "[]";
			}
			resultMap = contentServiceBean.insertContent(cmContentVo,picJson);
		}

		//resultMap = contentServiceBean.insertContent(cmContentVo,picJson);
		return resultMap;
	}

	@RequestMapping("/content/insert_content_csbmjm")
	@ResponseBody
	public Object insertContentCsbmjm(CmContentVo cmContentVo,String picJson, HttpSession session) throws UnsupportedEncodingException {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		int orgId = 0;
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			orgId=userVo.getOrgId();
		}
		CmContentLedgerVo cmContentLedger=new CmContentLedgerVo();
		cmContentLedger.setF_ccl_ip(String.valueOf(session.getAttribute(ConstantUtils.SESSION_USER_IP)));
		cmContentLedger.setF_ccl_user(userVo.getF_pu_name());
		cmContentVo.setCmContentLedger(cmContentLedger);
		cmContentVo.setOrgId(orgId);
		cmContentVo.setOrgIds(userVo.getOrgIds());
		if (cmContentVo.getF_cc_encryption() == 1) {
			//解码
			Base64.Decoder decoder = Base64.getDecoder();
			byte[] bytes = decoder.decode(cmContentVo.getF_cc_desc());
			String F_cc_desc_decode = new String(bytes, "UTF-8");
			cmContentVo.setF_cc_desc(F_cc_desc_decode);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>();

		if(cmContentVo.getF_cc_type()==100){
			int existingAccount = contentServiceBean.verificationExistingAccounts(cmContentVo,1);
			if(existingAccount==0){
				if ("".equals(picJson)) {
					picJson = "[]";
				}
				resultMap = contentServiceBean.insertContent(cmContentVo,picJson);
			}else{
				resultMap.put("success", false);
				resultMap.put("result","用户数录入已超过"+existingAccount+"个，若要继续录入，需向省公司发起申请！");
			}
		}else{
			if ("".equals(picJson)) {
				picJson = "[]";
			}
			resultMap = contentServiceBean.insertContent(cmContentVo,picJson);
		}

		//resultMap = contentServiceBean.insertContent(cmContentVo,picJson);
		return resultMap;
	}

	@RequestMapping("/content/insert_batch_content")
	@ResponseBody
	public Object insertBatchContent(
			@RequestParam(value = "uploadify") MultipartFile file ,
			@RequestParam(value = "f_cc_companyid") String f_cc_companyid,
			@RequestParam(value = "f_cc_programatype") String f_cc_programatype,
			@RequestParam(value = "chooseOption1") int chooseOption1,
			@RequestParam(value = "chooseOption2") int chooseOption2,
			HttpServletResponse response) throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		InputStream is = file.getInputStream();
		if (FileType.checkFileType(is, allowedFileType)) {
			int successCount = 0;
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			JSONArray emptyArr = new JSONArray();
			StringBuffer str = new StringBuffer();
			StringBuffer str2 = new StringBuffer();
			InputStream inputStream = null;
			try {
				inputStream = file.getInputStream();
				LOG.debug("批量导入content开始");
				List<Map<String, Object>> list = ExcelUtil.parseExcel(inputStream, ExcelUtil.ALLOWED_EMPTY_MODE, new String[]{"内容名称", "总集数"});
				LOG.debug("解析excel完成, 开始循环入库");
				//10年后
				String offlineDate = "2099-12-31 23:59:59";
				String onlineDate = format.format(new Date());
				for (int i = 0; i < list.size(); i++) {
					try {
						Map<String, Object> m = list.get(i);
						CmContentVo content = ConvertBeanUtil.getBeanFromMap(m, CmContentVo.class, CmContentVo.class.getName());
						if (content.getF_cc_obligate1() != null && content.getF_cc_obligate1().length() > 0) {
							if (content.getF_cc_obligate1().contains("/")) {
								String[] obligate1String = content.getF_cc_obligate1().split("/");
								String newObligate1String = obligate1String[0] + " " + obligate1String[1];
								content.setF_cc_obligate1(newObligate1String);
							}
						}
						String catalogIds = content.getCatalogIds();
						content.setF_cc_isfree(0);
						content.setF_cc_status(2);
						content.setF_cc_onlinedate(onlineDate);
						content.setF_cc_offlinedate(offlineDate);
						content.setF_cc_companyid(f_cc_companyid);
						content.setF_cc_programatype(f_cc_programatype);
						boolean success = false;
						if (chooseOption1 == 1) {//帐号导入 去重
							CmContentVo cc = new CmContentVo();
							cc.setF_cc_type(chooseOption2);
							cc.setF_cc_name(content.getF_cc_name());
							Map<String, Object> result = contentServiceBean.selectContentList(cc, 1, 1);
							content.setF_cc_status(1);
							if ((Integer) result.get("total") > 0) {
								str.append("帐号重复: ").append(content.getF_cc_name()).append("<br/>");
							} else {
								Map<String, Object> resultMap = contentServiceBean.insertContent(content, emptyArr.toJSONString());
								success = (Boolean) resultMap.get("success");

								if (catalogIds != null && catalogIds.length() > 0) {
									if (catalogIds.contains("-")) {
										String[] catalogIdsString = catalogIds.split("-");
										for (int cataId = 0; cataId < catalogIdsString.length; cataId++) {
											contentServiceBean.insertBind(Integer.valueOf(catalogIdsString[cataId]), Integer.valueOf(resultMap.get("f_cc_id").toString()), "0");
										}
									} else {
										contentServiceBean.insertBind(Integer.parseInt(catalogIds), Integer.valueOf(resultMap.get("f_cc_id").toString()), "0");
									}
								}

							}
						} else {//默认 直接导入
							Map<String, Object> resultMap = contentServiceBean.insertContent(content, emptyArr.toJSONString());
							success = (Boolean) resultMap.get("success");

							if (catalogIds != null && catalogIds.length() > 0) {
								if (catalogIds.contains("-")) {
									String[] catalogIdsString = catalogIds.split("-");
									for (int cataId = 0; cataId < catalogIdsString.length; cataId++) {
										contentServiceBean.insertBind(Integer.valueOf(catalogIdsString[cataId]), Integer.valueOf(resultMap.get("f_cc_id").toString()), "0");
									}
								} else {
									contentServiceBean.insertBind(Integer.parseInt(catalogIds), Integer.valueOf(resultMap.get("f_cc_id").toString()), "0");
								}
							}

						}

						successCount += (success ? 1 : 0);
						if (!success) {
							str2.append(i + 1).append(",");
						}
					} catch (Exception e) {
						LOG.error("", e);
						//str.append("第").append(i+1).append("条 出错! ").append(e.getMessage()).append("<br/>");
					}
				}
				LOG.debug("批量完成");
				if (str2.length() > 0) {
					str.append("第").append(str2.substring(0, str2.length() - 1)).append("条导入失败! ").append("<br/>");
				}
				str.append("批量导入完成， 一共：").append(list.size()).append("条数据, 成功").append(successCount).append(", 失败").append(list.size() - successCount);
			} catch (Exception e) {
				LOG.error("", e);
				//str.append("批量导入失败: " + e.getMessage());
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
					}
				}
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
					}
				}
			}
			return str.toString();
		} else {
			return "";

		}
	}

/*	@RequestMapping("/content/xl_batch_content")
	@ResponseBody
	public Object xlBatchContent(
			@RequestParam(value = "uploadify") MultipartFile file ,HttpServletResponse response) throws Exception {
		if (FileType.checkFileType(file.getInputStream(), allowedFileType)) {
			response.setContentType("text/html;charset=UTF-8");
			int successCount = 0;
			StringBuffer str = new StringBuffer();
			StringBuffer str2 = new StringBuffer();
			try {
				LOG.debug("批量导入content开始");
				List<Map<String, Object>> list = ExcelUtil.parseExcel(file.getInputStream(), ExcelUtil.ALLOWED_EMPTY_MODE, new String[]{"内容名称", "内容code", "预留字段1", "第三方广告id"});
				LOG.debug("解析excel完成, 开始循环入库");
				for (int i = 0; i < list.size(); i++) {
					Map<String, Object> m = list.get(i);
					Object f_cc_name = m.get("内容名称");
					Object f_cc_code = m.get("内容code");
					Object f_cc_obligate1 = m.get("预留字段1");
					Object f_ca_id = m.get("第三方广告id");
					int flag = contentServiceBean.updateBatchCount(f_cc_name.toString(), f_cc_code.toString(), f_cc_obligate1.toString());
					System.out.println(flag);
					if (flag > 0) {
						successCount++;
						CmAdvertiseVo cmAdvertiseVo = new CmAdvertiseVo();
						cmAdvertiseVo.setF_ca_id(Integer.valueOf(f_ca_id.toString()));
						cmAdvertiseVo.setF_ca_status(1);
						hotelAdvertiseDao.deleteAdvertise(cmAdvertiseVo);
					} else {
						str2.append(i + 1).append(",");
					}
				}
				LOG.debug("批量完成");
				if (str2.length() > 0) {
					str.append("第").append(str2.substring(0, str2.length() - 1)).append("条导入失败! ").append("<br/>");
				}
				str.append("批量导入完成， 一共：").append(list.size()).append("条数据, 成功").append(successCount).append(", 失败").append(list.size() - successCount);
			} catch (Exception e) {
				LOG.error("", e);
				//str.append("批量导入失败: " + e.getMessage());
			}
			return str.toString();
		} else {
			return "";
		}
	}*/


/*	@RequestMapping("/content/industry_batch_insert_content")
	@ResponseBody
	public Object industryBatchInsertContent(
			@RequestParam(value = "uploadify") MultipartFile file ,
			@RequestParam(value = "f_cc_companyid") String f_cc_companyid,
			HttpServletResponse response)throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		if(FileType.checkFileType(file.getInputStream(), allowedFileType)){
			String result = contentServiceBean.industryBatchInsertContent(file, f_cc_companyid);
			return result;
		}else{
			return null;
		}

	}*/


	/**
	 * 批量录入剧集
	 * @param file	excel文件
	 * @param f_ssc_contentid 父级id
	 * @param response
	 * @return
	 */
/*	@RequestMapping("/content/insert_batch_series")
	@ResponseBody
	public Object insertBatchSeries(@RequestParam(value = "uploadify") MultipartFile file , @RequestParam(value = "f_ssc_contentid") int f_ssc_contentid, HttpServletResponse response) throws Exception {
		if (FileType.checkFileType(file.getInputStream(), allowedFileType)) {
			response.setContentType("text/html;charset=UTF-8");
			int success = 0;
			//DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			JSONArray emptyArr = new JSONArray();
			StringBuffer str = new StringBuffer();
			try {
				LOG.debug("批量导入series开始");
				List<Map<String, Object>> list = ExcelUtil.parseExcel(file.getInputStream(), ExcelUtil.ALLOWED_EMPTY_MODE, new String[]{"剧集名称", "剧集code", "排序值"});
				LOG.debug("解析excel完成, 开始循环入库");
				//Calendar c = Calendar.getInstance();
				//c.set(2026, 0, 1, 0, 0, 0);
				//String offlineDate = format.format(c.getTime());
				//String onlineDate = format.format(new Date());
				for (int i = 0; i < list.size(); i++) {
					try {
						Map<String, Object> m = list.get(i);
						CmSeriesVo content = ConvertBeanUtil.getBeanFromMap(m, CmSeriesVo.class, CmSeriesVo.class.getName());
						content.setF_ssc_isfree(0);//默认收费
						content.setF_ssc_status(1);//默认为生效
						content.setF_ssc_contentid(f_ssc_contentid);
						int flag = contentServiceBean.insertSeries(content, emptyArr.toJSONString());
						success += (flag > 0 ? 1 : 0);
					} catch (Exception e) {
						LOG.error("", e);
						//str.append("第").append(i+1).append("条 出错! ").append(e.getMessage()).append("<br/>");
					}
				}
				LOG.debug("批量完成");
				str.append("批量导入完成， 一共：").append(list.size()).append("条数据, 成功").append(success).append(", 失败").append(list.size() - success);
			} catch (Exception e) {
				LOG.error("", e);
				//	str.append("批量导入失败: " + e.getMessage());
			}
			return str.toString();
		} else {
			return "";
		}
	}*/

	@RequestMapping("/content/insert_series")
	@ResponseBody
	public Object insertSeries(CmSeriesVo cmSeriesVo,String picJson) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.insertSeries(cmSeriesVo,picJson);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","新增剧集成功!");
		} else {
			resultMap.put("success", true);
			resultMap.put("result","新增剧集失败!flag="+flag);
		}
		return resultMap;
	}

	@RequestMapping("/content/select_content")
	@ResponseBody
	public Object selectContent(int f_cc_id) {
		CmContentVo cmContentVo = contentServiceBean.selectContent(f_cc_id);
		List<CmContentOrgVo> ccoList = contentServiceBean.selectOrgBind(f_cc_id);
		String ids="";
		String names="";
		for(int i=0;i<ccoList.size();i++){
			if(i+1==ccoList.size()){
				ids+=ccoList.get(i).getF_cco_orgId();
				names+=ccoList.get(i).getF_cco_orgName();
			}else{
				ids+=ccoList.get(i).getF_cco_orgId()+",";
				names+=ccoList.get(i).getF_cco_orgName()+",";
			}
		}
		cmContentVo.setF_cc_companyid(ids);
		cmContentVo.setF_cc_companyname(names);

		List<CmContentProductVo> ccpList = contentServiceBean.selectProductBind(f_cc_id);
		ids="";
		names="";
		for(int i=0;i<ccpList.size();i++){
			if(i+1==ccpList.size()){
				ids+=ccpList.get(i).getF_ccp_productId();
				names+=ccpList.get(i).getF_sp_name();
			}else{
				ids+=ccpList.get(i).getF_ccp_productId()+",";
				names+=ccpList.get(i).getF_sp_name()+",";
			}
		}
		cmContentVo.setF_cc_productid(ids);
		cmContentVo.setF_cc_productname(names);

		List<CmCatalogContentVo> cccList = contentServiceBean.selectCatalogBind(f_cc_id);
		String catalogIds="";
		String catalogNames="";
		for(int i=0;i<cccList.size();i++){
			if(i+1 == cccList.size()){
				catalogIds+=cccList.get(i).getF_ccc_catalogid();
			}else{
				catalogIds+=cccList.get(i).getF_ccc_catalogid()+",";
			}
		}
		if(!"".equals(catalogIds)){
			List<CmCatalogVo> cmCatalogVoList = catalogServiceBean.selectCatalogContentList(catalogIds);
			for(int i=0;i<cmCatalogVoList.size();i++){
				if(i+1 == cmCatalogVoList.size()){
					catalogNames+=cmCatalogVoList.get(i).getF_catalog_id()+"_"+cmCatalogVoList.get(i).getF_catalog_name();
				}else{
					catalogNames+=cmCatalogVoList.get(i).getF_catalog_id()+"_"+cmCatalogVoList.get(i).getF_catalog_name()+",";
				}
			}
		}
		cmContentVo.setCatalogNames(catalogNames);

		return cmContentVo;
	}

	@RequestMapping("/content/select_series")
	@ResponseBody
	public Object selectSeries(int f_ssc_id) {
		CmSeriesVo cmSeriesVo = contentServiceBean.selectSeries(f_ssc_id);
		return cmSeriesVo;
	}

	@RequestMapping("/content/update_content")
	@ResponseBody
	public Object updateContent( HttpSession session,CmContentVo cmContentVo , String videoJson , String picJson ,String status) throws UnsupportedEncodingException {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		int orgId = 0;
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			orgId=userVo.getOrgId();
		}

		cmContentVo.setOrgId(orgId);
		cmContentVo.setOrgIds(userVo.getOrgIds());
		cmContentVo.setRoleId(userVo.getRoleIds());
		if (cmContentVo.getF_cc_encryption() == 1) {
			//解码
			Base64.Decoder decoder = Base64.getDecoder();
			byte[] bytes = decoder.decode(cmContentVo.getF_cc_desc());
			String F_cc_desc_decode = new String(bytes, "UTF-8");
			cmContentVo.setF_cc_desc(F_cc_desc_decode);
		}
		//操作台账
		CmContentLedgerVo cmContentLedger=new CmContentLedgerVo();
		cmContentLedger.setF_ccl_ip(String.valueOf(session.getAttribute(ConstantUtils.SESSION_USER_IP)));
		cmContentLedger.setF_ccl_user(userVo.getF_pu_name());
		cmContentVo.setCmContentLedger(cmContentLedger);

		Map<String,Object> resultMap = new HashMap<String,Object>();
		String picType=picTypeContent;
		if(cmContentVo.getF_cc_type()==20&&cmContentVo.getSourceFlag()==5){//模板行业需要解开
			picType=picTypeModel;
		}

		if(cmContentVo.getF_cc_type()==100){
			int existingAccount = contentServiceBean.verificationExistingAccounts(cmContentVo,1);
			if(existingAccount==0){
				if ("".equals(picJson)) {
					picJson = "[]";
				}
				resultMap = contentServiceBean.updateContent(cmContentVo,picJson,picType,status);
			}else{
				resultMap.put("success", false);
				resultMap.put("result","用户数录入已超过"+existingAccount+"个，若要继续录入，需向省公司发起申请！");
			}
		}else{
			if ("".equals(picJson)) {
				picJson = "[]";
			}
			resultMap = contentServiceBean.updateContent(cmContentVo,picJson,picType,status);
		}

		//resultMap = contentServiceBean.updateContent(cmContentVo,picJson,picType,status);
		return resultMap;
	}

	@RequestMapping("/content/update_content_csbmjm")
	@ResponseBody
	public Object updateContentCsbmjm( HttpSession session,CmContentVo cmContentVo , String videoJson , String picJson ,String status) throws UnsupportedEncodingException {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		int orgId = 0;
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			orgId=userVo.getOrgId();
		}

		cmContentVo.setOrgId(orgId);
		cmContentVo.setOrgIds(userVo.getOrgIds());
		cmContentVo.setRoleId(userVo.getRoleIds());
		if (cmContentVo.getF_cc_encryption() == 1) {
			//解码
			Base64.Decoder decoder = Base64.getDecoder();
			byte[] bytes = decoder.decode(cmContentVo.getF_cc_desc());
			String F_cc_desc_decode = new String(bytes, "UTF-8");
			cmContentVo.setF_cc_desc(F_cc_desc_decode);
		}
		//操作台账
		CmContentLedgerVo cmContentLedger=new CmContentLedgerVo();
		cmContentLedger.setF_ccl_ip(String.valueOf(session.getAttribute(ConstantUtils.SESSION_USER_IP)));
		cmContentLedger.setF_ccl_user(userVo.getF_pu_name());
		cmContentVo.setCmContentLedger(cmContentLedger);

		Map<String,Object> resultMap = new HashMap<String,Object>();
		String picType=picTypeContent;
		if(cmContentVo.getF_cc_type()==20&&cmContentVo.getSourceFlag()==5){//模板行业需要解开
			picType=picTypeModel;
		}

		if(cmContentVo.getF_cc_type()==100){
			int existingAccount = contentServiceBean.verificationExistingAccounts(cmContentVo,1);
			if(existingAccount==0){
				if ("".equals(picJson)) {
					picJson = "[]";
				}
				resultMap = contentServiceBean.updateContent(cmContentVo,picJson,picType,status);
			}else{
				resultMap.put("success", false);
				resultMap.put("result","用户数录入已超过"+existingAccount+"个，若要继续录入，需向省公司发起申请！");
			}
		}else{
			if ("".equals(picJson)) {
				picJson = "[]";
			}
			resultMap = contentServiceBean.updateContent(cmContentVo,picJson,picType,status);
		}

		//resultMap = contentServiceBean.updateContent(cmContentVo,picJson,picType,status);
		return resultMap;
	}

	@RequestMapping("/content/review_content")
	@ResponseBody
	public Object reviewContent( HttpSession session,CmContentVo cmContentVo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		//操作台账
		CmContentLedgerVo cmContentLedger=new CmContentLedgerVo();
		cmContentLedger.setF_ccl_ip(String.valueOf(session.getAttribute(ConstantUtils.SESSION_USER_IP)));
		cmContentLedger.setF_ccl_user(userVo.getF_pu_name());
		cmContentVo.setCmContentLedger(cmContentLedger);

		int flag = contentServiceBean.reviewContent(cmContentVo,userVo);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","审核成功!");
		} else {
			resultMap.put("success", false);
			if(flag==-4){
				resultMap.put("result","审核失败,无该审核状态!flag="+flag);
			}else if(flag==-3){
				resultMap.put("result","审核失败,无审核权限!flag="+flag);
			}else if(flag==0){
				resultMap.put("result","审核失败!flag="+flag);
			}else if(flag==-10){
				resultMap.put("result","审核失败,模板存在危害平台代码!flag="+flag);
			}else if(flag==-9){
				resultMap.put("result","审核失败,模板解压失败!flag="+flag);
			}else{
				resultMap.put("result","审核失败!flag="+flag);
			}

		}
		return resultMap;
	}
	@RequestMapping("/content/pretrialContent")
	@ResponseBody
	public Object pretrialContent( HttpSession session,CmContentVo cmContentVo){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		int flag = contentServiceBean.pretrialContent(cmContentVo,userVo);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","审核解压成功!");
		} else {
			resultMap.put("success", false);
			if(flag==0){
				resultMap.put("result","审核解压失败!flag="+flag);
			}else if(flag==-9){
				resultMap.put("result","审核解压失败,模板解压失败!flag="+flag);
			}else{
				resultMap.put("result","审核解压失败!flag="+flag);
			}
		}
		return null;
	}
	@RequestMapping("/content/update_series")
	@ResponseBody
	public Object updateSeries(CmSeriesVo cmSeriesVo,String picJson) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.updateSeries(cmSeriesVo,picJson,picTypeSeries);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","修改剧集成功!");
		} else {
			resultMap.put("success", true);
			resultMap.put("result","修改剧集失败!flag="+flag);
		}
		return resultMap;
	}

	/*
	 * 	随动页管理后台数据查询接口
	 * */
	@RequestMapping("/gdsd/getOnlyRecommendInfo")
	@ResponseBody
	public Object selectBindContentGd(CmContentVo cmContentVo,String moduleCode,Integer status,int page,int rows,String signature,String platform,String source,String date){
		Map<String,Object> resultMap = new HashMap<>();
		String key=SystemConfigUtils.getSysConfig("interface_key");
		String signature_=new String(DigestUtils.md5Hex((platform + source+date + key)));
		LOG.debug(platform + source+date + key);
		LOG.debug(signature_+"|==|"+signature);
		if(signature!=null&&signature.equals(signature_)){
			LOG.debug("验证通过");
			SysConfigVo sysConfigVo = systemConfigService.selectSystemByKey(moduleCode);
			int f_ccc_catalogid = Integer.parseInt(sysConfigVo.getF_sc_value().split(",")[0]);
			SysConfigVo configVo = systemConfigService.selectSystemByKey("cloud_two_plane_url");
			String img_url = configVo.getF_sc_value();
			if(status==null)status = 0;
			cmContentVo.setF_ccc_sdStatus(status);
			Map<String,Object> map =  contentServiceBean.selectBindContent(cmContentVo,f_ccc_catalogid,page,rows);
			List<CmContentVo> cmContentVos = (List<CmContentVo>) map.get("rows");
			List<Map<String,Object>> vods = new ArrayList<>();
			cmContentVos.forEach(content -> {
				Map<String,Object> temap = new HashMap<>();
				temap.put("id",content.getF_cc_id());
				if(StringUtils.isEmpty(content.getF_cc_picurl())){
					temap.put("imgurl","");
				}else{
					String[] pics = content.getF_cc_picurl().split(",");
					StringBuilder sb = new StringBuilder();
					IntStream.range(0,pics.length).forEach(index -> {
						sb.append(img_url).append(pics[index]).append(",");
					});
					if(StringUtils.isEmpty(sb.toString())){
						temap.put("imgurl","");
					}else{
						temap.put("imgurl",sb.toString().substring(0,sb.length()-1));
					}
				}
				temap.put("linkurl",content.getF_cc_desc().replaceAll("<[.[^<]]*>", ""));
				temap.put("title",content.getF_cc_name());
				temap.put("status",content.getF_ccc_sdStatus());
				temap.put("index",content.getF_ccc_viewindex());
				temap.put("date", content.getF_ccc_createdate());
				temap.put("company", content.getF_cc_companyname());
				vods.add(temap);
			});
			resultMap.put("retCode",100);
			resultMap.put("retMsg","处理成功");
			resultMap.put("total",map.get("total"));
			resultMap.put("vods",vods);
		}else{
			resultMap.put("retCode",10013);
			resultMap.put("retMsg","操作失败，验证标识错误"+signature_);
		}
		return resultMap;
	}
	/*
	 *  随动内容审核接口
	 * */
	@RequestMapping("/gdsd/changeRecommend")
	@ResponseBody
	@Transactional
	public Object updateSdStatus(String moduleCode,int id,int status,Integer index,String signature,String platform,String source,String date){
		CmCatalogContentVo cmCatalogContentVo = new CmCatalogContentVo();
		Map<String,Object> resultMap = new HashMap<>();
		String key=SystemConfigUtils.getSysConfig("interface_key");
		String signature_=new String(DigestUtils.md5Hex((platform + source+date + key)));
		LOG.debug(platform + source+date + key);
		LOG.debug(signature_+"|==|"+signature);
		if(signature!=null&&signature.equals(signature_)){
			SysConfigVo sysConfigVo = systemConfigService.selectSystemByKey(moduleCode);
			if(sysConfigVo == null){
				resultMap.put("result",102);
				resultMap.put("msg","未定义推荐位");
				return resultMap;
			}
			int f_ccc_catalogid = Integer.parseInt(sysConfigVo.getF_sc_value().split(",")[0]);
			int maxResults = Integer.parseInt(sysConfigVo.getF_sc_value().split(",")[1]);
			if(index==null) index = 0;
			cmCatalogContentVo.setF_ccc_catalogid(f_ccc_catalogid);
			cmCatalogContentVo.setF_ccc_contentid(id);
			cmCatalogContentVo.setF_ccc_sdStatus(status);
			cmCatalogContentVo.setF_ccc_viewindex(index);
			CmContentVo cmContentVo = new CmContentVo();
			cmContentVo.setF_ccc_sdStatus(1);
			if(status == 2 || status == 3){//下线/历史，判断线上数据够不够，不够不让下线
				int onlineCount = contentServiceBean.findBindContentCount(cmContentVo,f_ccc_catalogid);
				//如果该推荐位的上线内容小于等于当前推推荐位推荐内容，不让下线（先上线后下线）
				if(onlineCount<=maxResults){
					resultMap.put("result",105);
					resultMap.put("msg","内容不足，无法下线");
					return resultMap;
				}
				int flag = contentServiceBean.updateViewIndex(cmCatalogContentVo);
				if(flag > 0){
					resultMap.put("result",100);
					resultMap.put("msg","操作成功");
				}else{
					resultMap.put("result",103);
					resultMap.put("msg","操作失败");
				}
				return resultMap;
			}else if(status == 1){//上线
				Map<String,Object> contentMap = contentServiceBean.selectBindContent(cmContentVo,f_ccc_catalogid,1,1000);
				List<CmContentVo> cmContentVos = (List<CmContentVo>) contentMap.get("rows");
				for(CmContentVo contentVo : cmContentVos){
					if(contentVo.getF_cc_id() == id){
						if(index == contentVo.getF_ccc_viewindex()){
							resultMap.put("result",104);
							resultMap.put("msg","操作失败,请勿重复绑定");
						}else{
							//更新排序值
							contentServiceBean.updateViewIndex(cmCatalogContentVo);
							resultMap.put("result",100);
							resultMap.put("msg","操作成功");
						}
						return resultMap;
					}
				}
				//更新上线时，如果有对应的排序值，则下线该内容，否则下线最后一个上线内容
				int flag;
				if((Integer)contentMap.get("total") < maxResults){//该推荐位上线值小于推荐位预设值，直接上线
					cmCatalogContentVo.setF_ccc_viewindex(index);
					flag = contentServiceBean.updateViewIndex(cmCatalogContentVo);
					if(flag >= 0){
						resultMap.put("result",100);
						resultMap.put("msg","操作成功");
					}else{
						resultMap.put("result",103);
						resultMap.put("msg","操作失败");
					}
					return resultMap;
				}
				int minIndex = cmContentVos.get(0).getF_ccc_viewindex();
				CmContentVo contentVo1 = new CmContentVo();
				for(CmContentVo contentVo : cmContentVos){
					if(contentVo.getF_ccc_viewindex() == index && index > 0 ){
						minIndex = index;
						contentVo1 = contentVo;
						break;
					}else{
						//判断最小排序
						if(minIndex >= contentVo.getF_ccc_viewindex()){
							if(index == 0){
								minIndex = contentVo.getF_ccc_viewindex();
							}else {
								minIndex = index;
							}
							contentVo1 = contentVo;
						}
					}
				}
				cmCatalogContentVo.setF_ccc_viewindex(minIndex);
				flag = contentServiceBean.updateViewIndex(cmCatalogContentVo);
				//不是同一条内容时，更新原来的数据为历史
				if(contentVo1.getF_cc_id() != cmCatalogContentVo.getF_ccc_contentid()){
					cmCatalogContentVo.setF_ccc_contentid(contentVo1.getF_cc_id());
					cmCatalogContentVo.setF_ccc_sdStatus(3);
					cmCatalogContentVo.setF_ccc_viewindex(0);
					flag = contentServiceBean.updateViewIndex(cmCatalogContentVo);
				}
				//做一道保险将上线内容之外的数据更新成历史
				int flag2 = contentServiceBean.updateSdStatusHistory(f_ccc_catalogid,maxResults);
				if(flag >= 0 && flag2 >= 0){
					resultMap.put("result",100);
					resultMap.put("msg","操作成功");
				}else{
					resultMap.put("result",103);
					resultMap.put("msg","操作失败");
				}
			}
		}else{
			resultMap.put("result",10013);
			resultMap.put("msg","操作失败，验证标识错误"+signature_);
		}
		return resultMap;
	}


	@RequestMapping("/content/content_bind")
	@ResponseBody
	public Object selectBindContent(CmContentVo cmContentVo, HttpSession session,int bind_status,int f_ccc_catalogid,int page,int rows) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			String orgId=userVo.getOrgIds();
			if(orgId==null){
				orgId=userVo.getOrgId()+"";
			}
			if(cmContentVo.getF_cc_companyid()==null||"".equals(cmContentVo.getF_cc_companyid())||cmContentVo.getF_cc_companyid().equals(orgId)){
				cmContentVo.setF_cc_companyid(orgId);
			}else{
				boolean flag=true;
				String[] orgIds=orgId.split(",");
				for(int i=0;i<orgIds.length;i++){
					if(cmContentVo.getF_cc_companyid().equals(orgIds[i])){
						flag=false;
						break;
					}
				}
				if(flag){//改企业不在列表内则不可查询
					cmContentVo.setF_cc_companyid(orgId);
				}

			}

		}

		Map<String,Object> bindMap = new HashMap<String, Object>();
		if(bind_status ==1) {
			bindMap = contentServiceBean.selectBindContent(cmContentVo,f_ccc_catalogid,page,rows);
		} else if (bind_status ==2) {
			bindMap = contentServiceBean.selectUnBindContent(cmContentVo,f_ccc_catalogid,page,rows);
		}
		return bindMap;
	}

	@RequestMapping("/content/delete_bind")
	@ResponseBody
	public Object deleteBind(int f_ccc_catalogid,int f_ccc_contentid, HttpSession session) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		String orgId = "0";
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			orgId=userVo.getOrgIds();
			if(orgId==null){
				orgId=userVo.getOrgId()+"";
			}
		}
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.deleteBind(f_ccc_catalogid,f_ccc_contentid,orgId);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","解除绑定成功!");
		} else if(flag == -5) {
			resultMap.put("success", false);
			resultMap.put("result","解除绑定失败! 无此目录权限!");
		} else{
			resultMap.put("success", false);
			resultMap.put("result","解除绑定失败!flag="+flag);
		}
		return resultMap;
	}

	@RequestMapping("/content/insert_bind")
	@ResponseBody
	public Object insertBind(int f_ccc_catalogid,int f_ccc_contentid, HttpSession session) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		String orgId = "0";
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			orgId=userVo.getOrgIds();
			if(orgId==null){
				orgId=userVo.getOrgId()+"";
			}
		}
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.insertBind(f_ccc_catalogid,f_ccc_contentid, orgId);
		resultMap.put("flag", flag);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","新增绑定成功!");
		} else if(flag == -5) {
			resultMap.put("success", false);
			resultMap.put("result","新增绑定失败! 无此目录权限!");
		} else{
			resultMap.put("success", false);
			resultMap.put("result","新增绑定失败!flag="+flag);
		}
		return resultMap;
	}

	@RequestMapping("/content/batch_bind")
	@ResponseBody
	public Object batchBind(int f_ccc_catalogid,String ids, HttpSession session) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		String orgId = "0";
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			orgId=userVo.getOrgIds();
			if(orgId==null){
				orgId=userVo.getOrgId()+"";
			}
		}
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.batchBind(f_ccc_catalogid,ids,orgId);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量绑定成功!");
		} else if(flag == -5) {
			resultMap.put("success", false);
			resultMap.put("result","批量绑定失败! 无此目录权限!");
		} else{
			resultMap.put("success", false);
			resultMap.put("result","批量绑定失败!flag="+flag);
		}
		return resultMap;
	}

	@RequestMapping("/content/batch_unbind")
	@ResponseBody
	public Object batchUnBind(int f_ccc_catalogid,String ids, HttpSession session) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		String orgId = "0";
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			orgId=userVo.getOrgIds();
			if(orgId==null){
				orgId=userVo.getOrgId()+"";
			}
		}
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.batchUnBind(f_ccc_catalogid,ids,orgId);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量解绑成功!");
		} else if(flag == -5) {
			resultMap.put("success", false);
			resultMap.put("result","批量解绑失败! 无此目录权限!");
		} else{
			resultMap.put("success", false);
			resultMap.put("result","批量解绑失败!flag="+flag);
		}
		return resultMap;
	}

	@RequestMapping("/content/select_contentBindCatalog")
	@ResponseBody
	public Object selectContentBindCatalog(int f_cc_id) {
		List<CmCatalogVo> cataList = contentServiceBean.selectContentBindCatalog(f_cc_id);
		return cataList;
	}

	@RequestMapping("/content/select_catalogBind")
	@ResponseBody
	public Object selectCatalogBind(int f_ccc_contentid) {
		List<CmCatalogContentVo> cccList = contentServiceBean.selectCatalogBind(f_ccc_contentid);
		return cccList;
	}

	@RequestMapping("/content/select_orgBind")
	@ResponseBody
	public Object selectOrgBind(int f_cco_contentId) {
		List<CmContentOrgVo> ccoList = contentServiceBean.selectOrgBind(f_cco_contentId);
		return ccoList;
	}

	@RequestMapping("/content/update_catalogBind")
	@ResponseBody
	public Object updateCatalogBind(int f_ccc_contentid,String cataloginStr,String catalogdeStr, HttpSession session) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		String orgId = "0";
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){
			orgId=userVo.getOrgIds();
			if(orgId==null){
				orgId=userVo.getOrgId()+"";
			}
		}
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.updateCatalogBind(f_ccc_contentid,cataloginStr,catalogdeStr,orgId);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","修改绑定成功!");
		} else if(flag == -5) {
			resultMap.put("success", false);
			resultMap.put("result","修改绑定失败! 目录权限不足!");
			LOG.debug("修改绑定失败! 目录权限不足!userVo.getF_pu_name()="+userVo.getF_pu_name()+"=cataloginStr="+cataloginStr+"=catalogdeStr="+catalogdeStr);
		} else{
			resultMap.put("success", false);
			resultMap.put("result","修改绑定失败!flag="+flag);
		}
		return resultMap;
	}

	@RequestMapping("/content/select_viewindex")
	@ResponseBody
	public Object selectViewIndex(CmCatalogContentVo cmCatalogContentVo) {
		CmCatalogContentVo vo = contentServiceBean.selectViewIndex(cmCatalogContentVo);
		return vo;
	}

	@RequestMapping("/content/update_viewindex")
	@ResponseBody
	public Object updateViewIndex(CmCatalogContentVo cmCatalogContentVo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.updateViewIndex(cmCatalogContentVo);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","修改排序值成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","修改排序值失败!flag="+flag);
		}
		return resultMap;
	}
	@SuppressWarnings("unchecked")
	@RequestMapping("/content/content_excel_list")
	public void contentExcelList(CmContentVo cmContentVo, HttpSession session, int page, int rows,HttpServletResponse response){
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了
		if(userVo.getParentOrgId() > 0){//盗取
			String orgIds = pmsServiceBean.getAuthorityPmsOrgIds(userVo);
			if(cmContentVo.getF_cc_companyid()==null||"".equals(cmContentVo.getF_cc_companyid())||cmContentVo.getF_cc_companyid().equals(orgIds)){
				cmContentVo.setF_cc_companyid(orgIds);
			}else{
				boolean flag=true;
				String[] orgIdlist=orgIds.split(",");
				for(int i=0;i<orgIdlist.length;i++){
					if(cmContentVo.getF_cc_companyid().equals(orgIdlist[i])){
						flag=false;
						break;
					}
				}
				if(flag){//该企业不在列表内则不可查询
					try {
						response.getWriter().print("<h1 style='position: absolute;width: 100%;height: 100%;text-align: center;'>No permission.</h1>");
					} catch (IOException e) {
						LOG.error("无权限导数据"+userVo.getF_pu_realName(),e);
					}
					return;
				}

			}
		}
		Map<String,Object> contentListMap = new HashMap<String, Object>();
		contentListMap =  contentServiceBean.selectContentList(cmContentVo, 1, 50000);
		List<CmContentVo> data=(List<CmContentVo>)contentListMap.get("rows");
		List<MyEntry<String, String>> excelHeader=new ArrayList<MyEntry<String, String>>();
		if(cmContentVo.getSourceFlag()==1){//行业账号导出
			if(data.size()>=50000){
				try {
					response.getWriter().println("Quantity exceeded export limit. Total 50000");
					return;
				} catch (IOException e) {
					LOG.error("导出数量超标",e);
				}
			}
			excelHeader.add(new MyEntry<String,String>("f_cc_id","账号Id"));
			excelHeader.add(new MyEntry<String,String>("f_cc_name","客户账号 "));
			excelHeader.add(new MyEntry<String,String>("f_cc_searchname","姓名"));
			excelHeader.add(new MyEntry<String,String>("f_cc_companyname","所属行业客户"));
			excelHeader.add(new MyEntry<String,String>("f_cc_obligate1","客户地址"));
			excelHeader.add(new MyEntry<String,String>("f_cc_cdate","添加时间 "));
		}else if(cmContentVo.getSourceFlag()==2){//行业内容导出
			if(data.size()>=50000){
				try {
					response.getWriter().println("Quantity exceeded export limit. Total 50000");
					return;
				} catch (IOException e) {
					LOG.error("导出数量超标",e);
				}
			}
			for(int i=0;i<data.size();i++){
				switch (data.get(i).getF_cc_status()) {
					case 1:
						data.get(i).setStatus_name("上线");
						break;
					case 2:
						data.get(i).setStatus_name("市级待审核");
						break;
					case 3:
						data.get(i).setStatus_name("伪删除");
						break;
					case 4:
						data.get(i).setStatus_name("市级审核不通过");
						break;
					case 5:
						data.get(i).setStatus_name("待转码");
						break;
					case 6:
						data.get(i).setStatus_name("正在转码");
						break;
					case 7:
						data.get(i).setStatus_name("转码失败");
						break;
					case 13:
						data.get(i).setStatus_name("省级审核不通过");
						break;
					case 14:
						data.get(i).setStatus_name("牌照方审核");
						break;
					case 15:
						data.get(i).setStatus_name("牌照方审核不通过");
						break;
					case 16:
						data.get(i).setStatus_name("省级审核");
						break;
					case 17:
						data.get(i).setStatus_name("等待传输");
						break;
					case 18:
						data.get(i).setStatus_name("传输失败");
						break;
					case 19:
						data.get(i).setStatus_name("传输中");
						break;
					default:
						data.get(i).setStatus_name("未知");
						break;
				}
			}
			excelHeader.add(new MyEntry<String,String>("f_cc_id","内容Id"));
			excelHeader.add(new MyEntry<String,String>("f_cc_name","内容名称"));
			excelHeader.add(new MyEntry<String,String>("f_cc_code","内容code"));
			excelHeader.add(new MyEntry<String,String>("f_cc_companyname","所属行业客户"));
			excelHeader.add(new MyEntry<String,String>("status_name","内容状态"));
			excelHeader.add(new MyEntry<String,String>("f_cc_cdate","添加时间 "));
		}else{
			excelHeader.add(new MyEntry<String,String>("f_cc_id","内容Id"));
			excelHeader.add(new MyEntry<String,String>("f_cc_name","内容名称"));
			excelHeader.add(new MyEntry<String,String>("f_cc_code","内容code"));
			excelHeader.add(new MyEntry<String,String>("catalogNames","分类名称"));
			excelHeader.add(new MyEntry<String,String>("f_cc_actors","演员列表"));
			excelHeader.add(new MyEntry<String,String>("f_cc_type","内容类型"));
			excelHeader.add(new MyEntry<String,String>("f_cc_isfree","收费状况"));
			excelHeader.add(new MyEntry<String,String>("f_cc_end","完结状态"));
			excelHeader.add(new MyEntry<String,String>("f_cc_seriescount","总集数"));
			excelHeader.add(new MyEntry<String,String>("f_cc_onlinedate","上线时间 "));
			excelHeader.add(new MyEntry<String,String>("f_cc_offlinedate","下线时间 "));
			excelHeader.add(new MyEntry<String,String>("f_cc_obligate1","预留字段1 "));
			excelHeader.add(new MyEntry<String,String>("f_cc_obligate2","预留字段2"));
		}

		//boolean flag=
		ExcelUtil.generateExcel(data, excelHeader, response);
	}


	/**
	 * 批量审核
	 * @param ids
	 * @return
	 */
	@RequestMapping("/content/batch_review_content")
	@ResponseBody
	public Object batchReviewContent(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.batchReviewContent(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量审核成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量审核失败!flag="+flag);
		}
		return resultMap;
	}

	/**
	 * 批量下线
	 * @param ids
	 * @return
	 */
	@RequestMapping("/content/batch_unreview_content")
	@ResponseBody
	public Object batchUnReviewContent(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.batchUnReviewContent(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量下线成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量下线失败!flag="+flag);
		}
		return resultMap;
	}


	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/content/batch_delete_content")
	@ResponseBody
	public Object batchDeleteContent(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.batchDeleteContent(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量删除成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量删除失败!flag="+flag);
		}
		return resultMap;
	}

	@RequestMapping("/content/update_content_status")
	@ResponseBody
	public Object updateContentStatus(int f_cc_id,int f_cc_status) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.updateContentStatus(f_cc_id,f_cc_status);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","修改成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","修改失败!");
		}
		return resultMap;
	}

	/**
	 * 修改内容code值，用于视频手动转码
	 * @param f_cc_id
	 * @param f_cc_code
	 * @return
	 */
	@RequestMapping("/content/update_content_code")
	@ResponseBody
	public Object updateContentCode(int f_cc_id,String f_cc_code) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = contentServiceBean.updateContentCode(f_cc_id,f_cc_code);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","修改成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","修改失败!");
		}
		return resultMap;
	}
	/**
	 * 根据模板id查询关联企业列表
	 * @param
	 * @return
	 */
	@RequestMapping("/content/selectOrgListByModelId")
	@ResponseBody
	public Object selectOrgListByModelId(PmsOrgVo orgVo, int page, int rows){
		Map<String,Object> map = contentServiceBean.selectOrgListByModelId(orgVo,page,rows);
		return map;
	}
	/**
	 * 根据模板id查询关联企业列表
	 * @param
	 * @return
	 */
	@RequestMapping("/content/selectOrgListByModelOutExcel")
	@ResponseBody
	public void selectOrgListByModelOutExcel(PmsOrgVo orgVo,HttpServletResponse response){
		List<PmsOrgVo>  data=contentServiceBean.selectOrgListByModelIdAll(orgVo);
		List<MyEntry<String, String>> excelCpHeader=new ArrayList<MyEntry<String, String>>();
		excelCpHeader.add(new MyEntry<String,String>("f_po_id","序号"));
		excelCpHeader.add(new MyEntry<String,String>("f_po_name","行业客户"));
		excelCpHeader.add(new MyEntry<String,String>("f_po_createAt","增加时间"));
		boolean flag=ExcelUtil.generateExcel(data, excelCpHeader, response);
	}

	@RequestMapping("/content/vote_bind")
	@ResponseBody
	public Object selectVoteContent( HttpSession session,CmContentVo cmContentVo,String f_csvt_activityid,String f_catalog_starttime,String f_catalog_endtime,String min_num,String max_num,String f_catalog_id,String catalogIds,int page,int rows) {
		Map<String,Object> bindMap = new HashMap<String, Object>();
		bindMap = contentServiceBean.selectVoteContent(cmContentVo,f_csvt_activityid,min_num,max_num,f_catalog_starttime,f_catalog_endtime,f_catalog_id,catalogIds,page,rows);
		return bindMap;
	}
	@RequestMapping("/content/vote_bind_excel")
	public void vote_bind_excel( HttpSession session,CmContentVo cmContentVo,String f_csvt_activityid,String f_catalog_starttime,String f_catalog_endtime,String min_num,String max_num,String f_catalog_id,String catalogIds, HttpServletResponse response){
		Map<String,Object> bindMap = new HashMap<String, Object>();
		bindMap = contentServiceBean.vote_bind_excel(cmContentVo,f_csvt_activityid,min_num,max_num,f_catalog_starttime,f_catalog_endtime,f_catalog_id,catalogIds);
		List<CmContentVo> data=(List<CmContentVo>)bindMap.get("rows");
		List<MyEntry<String, String>> excelHeader=new ArrayList<MyEntry<String, String>>();
		excelHeader.add(new MyEntry<String,String>("f_cc_searchname","参赛编号"));
		excelHeader.add(new MyEntry<String,String>("f_cc_name","参赛选手"));
		excelHeader.add(new MyEntry<String,String>("f_csvt_activityid","活动编号"));
		excelHeader.add(new MyEntry<String,String>("f_csvt_votecount","总票数"));
		excelHeader.add(new MyEntry<String,String>("f_catalog_starttime","参赛时间"));
		excelHeader.add(new MyEntry<String,String>("f_catalog_endtime","结束时间"));
		excelHeader.add(new MyEntry<String,String>("catalogNames","分类"));
		ExcelUtil.generateExcel(data, excelHeader, response);
	}

	/**
	 * 根据id查询ac_vote_user_like
	 * @param
	 * @return
	 */
	@RequestMapping("/content/select_vote_user")
	@ResponseBody
	public Object selectVoteUser(AcVoteUserLikeVo acVoteUserLikeVo,int page,int rows) {
		Map<String,Object> bindMap = new HashMap<String, Object>();
		bindMap =contentServiceBean.selectVoteUser(acVoteUserLikeVo,page,rows);
		return bindMap;
	}

	@RequestMapping("/content/vote_excel")
	public void voteExcel(AcVoteUserLikeVo acVoteUserLikeVo,int page,int rows,HttpSession session, HttpServletResponse response){
		Map<String,Object> bindMap = new HashMap<String, Object>();
		bindMap = contentServiceBean.selectVoteUser(acVoteUserLikeVo, page, rows);
		List<AcVoteUserLikeVo> data=(List<AcVoteUserLikeVo>)bindMap.get("rows");
		List<MyEntry<String, String>> excelHeader=new ArrayList<MyEntry<String, String>>();
		excelHeader.add(new MyEntry<String,String>("f_avu_serverid","业务帐号"));
		excelHeader.add(new MyEntry<String,String>("f_avu_contentid","投票目标"));
		excelHeader.add(new MyEntry<String,String>("f_avu_votenum","用户票数"));
		excelHeader.add(new MyEntry<String,String>("f_avu_activityid","活动编号"));
		excelHeader.add(new MyEntry<String,String>("f_avu_source","投票来源"));
		excelHeader.add(new MyEntry<String,String>("f_avu_type","投票类型"));
		excelHeader.add(new MyEntry<String,String>("f_avu_memo","备注"));
		excelHeader.add(new MyEntry<String,String>("f_avu_obligate1","预留字段"));
		excelHeader.add(new MyEntry<String,String>("f_avu_obligate2","预留字段2"));
		excelHeader.add(new MyEntry<String,String>("f_avu_udate","更新时间"));
		excelHeader.add(new MyEntry<String,String>("f_avu_cdate","创建时间"));
		ExcelUtil.generateExcel(data, excelHeader, response);
	}

	@RequestMapping("/content/change_vote_count")
	@ResponseBody
	public Map<String, Object> changeVoteCount(int changeData,int contentid,String activityid) {
		CmVoteTotalVo cmVoteTotalVo =  contentServiceBean.selectVotaByContenId(contentid,activityid);
		LOG.debug("后台系统手工处理票数查询是否已有票数："+cmVoteTotalVo);
		if(cmVoteTotalVo !=null ){//修改
			int count = 0;
			int f_avu_type = 0;
			if(changeData>0){
				f_avu_type=1;
			}else if (changeData<0) {
				f_avu_type=2;
			}
			//count = cmVoteTotalVo.getF_csvt_votecount()+changeData;
			count = changeData;
			int flag = contentServiceBean.updateVote(count,contentid,activityid);
			Map<String, Object> resultMap = new HashMap<>();
			if(flag>0) {
				resultMap.put("success", true);
				resultMap.put("result","添加成功!");
			} else {
				resultMap.put("success", false);
				resultMap.put("result","添加失败!flag="+flag);
			}
			//为流水表ac_vote_user_like添加总数
			AcVoteUserLikeVo acVoteUserLikeVo  = new AcVoteUserLikeVo();
			acVoteUserLikeVo.setF_avu_serverid("057100000000");
			acVoteUserLikeVo.setF_avu_contentid(String.valueOf(contentid));
			acVoteUserLikeVo.setF_avu_memo("后台系统手工处理");
			acVoteUserLikeVo.setF_avu_activityid(activityid);
			acVoteUserLikeVo.setF_avu_votenum(count);
			acVoteUserLikeVo.setF_avu_source(10);
			acVoteUserLikeVo.setF_avu_type(f_avu_type);
			acVoteUserLikeVo.setF_avu_obligate1(1);

			contentServiceBean.insertVoteUser(acVoteUserLikeVo);
			return resultMap;
		}else{//新增
			CmVoteTotalVo newVo=new CmVoteTotalVo();
			newVo.setF_csvt_activityid(Integer.parseInt(activityid));
			newVo.setF_csvt_contentid(contentid);
			newVo.setF_csvt_votecount(changeData);
			int flag = contentServiceBean.insertVote(newVo);
			AcVoteUserLikeVo acVoteUserLikeVo = new AcVoteUserLikeVo();
			acVoteUserLikeVo.setF_avu_serverid("057100000000");
			acVoteUserLikeVo.setF_avu_contentid(String.valueOf(newVo.getF_csvt_contentid()));
			acVoteUserLikeVo.setF_avu_memo("后台系统手工处理");
			acVoteUserLikeVo.setF_avu_activityid(activityid);
			acVoteUserLikeVo.setF_avu_votenum(newVo.getF_csvt_votecount());
			acVoteUserLikeVo.setF_avu_source(10);
			acVoteUserLikeVo.setF_avu_type(1);
			contentServiceBean.insertVoteUser(acVoteUserLikeVo);
			Map<String, Object> resultMap = new HashMap<>();
			if(flag>0) {
				resultMap.put("success", true);
				resultMap.put("result","添加成功!");
			} else {
				resultMap.put("success", false);
				resultMap.put("result","添加失败!flag="+flag);
			}
			return resultMap;
		}
	}

	@RequestMapping("/content/insert_vote")
	@ResponseBody
	public Map<String, Object> insertVote(CmVoteTotalVo cmVoteTotalVo,String activityid) {
		cmVoteTotalVo.setF_csvt_activityid(Integer.parseInt(activityid));
		int flag = contentServiceBean.insertVote(cmVoteTotalVo);

		AcVoteUserLikeVo acVoteUserLikeVo = new AcVoteUserLikeVo();
		acVoteUserLikeVo.setF_avu_serverid("057100000000");
		acVoteUserLikeVo.setF_avu_contentid(String.valueOf(cmVoteTotalVo.getF_csvt_contentid()));
		acVoteUserLikeVo.setF_avu_memo("后台系统手工处理");
		acVoteUserLikeVo.setF_avu_activityid(activityid);
		acVoteUserLikeVo.setF_avu_votenum(cmVoteTotalVo.getF_csvt_votecount());
		acVoteUserLikeVo.setF_avu_source(10);
		acVoteUserLikeVo.setF_avu_type(1);
		contentServiceBean.insertVoteUser(acVoteUserLikeVo);
		Map<String, Object> resultMap = new HashMap<>();
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","添加成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","添加失败!flag="+flag);
		}
		return resultMap;
	}
}
